package command;

import java.util.HashSet;
import java.util.Iterator;

import model.impl.StationImpl;

import DTO.StationDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It checks that the received stations from client are unique,
 * and if they are create and save them.
 */
public class AdminCreateNewStation implements Command {

	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		model.Station stationImpl = new StationImpl();
		@SuppressWarnings("unchecked")
		HashSet<StationDTO> stations = (HashSet<StationDTO>) message.getObject();
		Iterator<StationDTO> iter = stations.iterator();
		while(iter.hasNext()) {
			stationImpl.saveUniqueStation(iter.next());
		}
		Protocol.Message answer = this.protocol.new Message("ok", null);
		recived.server.send(recived.socket, this.protocol.createMessage(answer));
	}

}
